#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct LinkNode{
	int data;
	struct LinkNode *next;
}*LiStack;
void InitStack(LiStack *s){
	printf("初始化中....\n");
	//生成一个头结点
	*s = (LiStack) malloc(sizeof(struct LinkNode));
	if(*s == NULL){
		printf("malloc fail\n");
		exit(0);
	}
	printf("初始化成功！！\n");
	(*s)->next = NULL;
}
bool StackEmpty(LiStack S){
	if(S->next == NULL){ //如果头节点下一个节点为空，就可以说明这个栈为空栈
		return true;
	}
	else {
		return false;
	}
}
bool Push(LiStack S,int x){
		/* 链栈是不会满的 */
//	if(S->top == MaxSize-1){  //栈满
//		return false;
//	}
	LiStack newNode = (LiStack) malloc(sizeof(struct LinkNode));
	newNode->data = x;
	//头插法
	newNode->next = S->next;
	S->next = newNode;
	return true;
}
bool Pop(LiStack S,int *x){
	if(StackEmpty(S)){
		return false;
	}
	LiStack temp = NULL;
	*x = S->next->data;
	//删除栈顶元素节点
	temp = S->next;
	S->next = temp->next;
	free(temp);
	return true;
}
bool GetTop(LiStack S,int *x){
	if(StackEmpty(S)){
		return false;
	}
	*x = S->next->data;
	return true;
}
int main(int argc, char *argv[]) {
	LiStack stack = NULL;
	InitStack(&stack);
	int *x;
	int choose;
	printf("1.入栈 2.出栈 3.获取栈顶元素:");
	while(scanf("%d",&choose)!=EOF && choose!=-1){
		if(choose == 1){
			int data;
			printf("输入入栈元素:");
			scanf("%d",&data);
			if(Push(stack, data)){
				printf("入栈成功！\n");
			}
			else{
				printf("入栈失败！栈已满\n");
			}
		}
		else if(choose == 2) {
			if(Pop(stack, x)){
				printf("出栈元素为:%d\n",*x);
			}
			else {
				printf("操作失败！栈已空！\n");
			}
		}
		else if(choose == 3) {
			if(GetTop(stack, x)){
				printf("栈顶元素为:%d\n",*x);
			}
			else {
				printf("操作失败！栈已空！\n");
			}
		}
		printf("1.入栈 2.出栈 3.获取栈顶元素:");
	}
}